package ua.at.tsvetkov.samizdat.services;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.AsyncTaskUpdate;
import ua.at.tsvetkov.util.Log;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;

public class SrvUpdate extends Service {

	private IBinder	binder;

	public class LocalBinder extends Binder {
		SrvUpdate getService() {
			return SrvUpdate.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.v(getString(R.string.SRV_UPDATE_BINDED));
		return binder;
	}

	@Override
	public void onCreate() {
		binder = new LocalBinder();
	}

	@Override
	public void onStart(Intent intent, int startid) {
		Log.v(getString(R.string.SRV_UPDATE_STARTED));
		AsyncTaskUpdate asyncTaskUpdate = new AsyncTaskUpdate(this);
		asyncTaskUpdate.execute();
	}

	@Override
	public void onDestroy() {
		Log.v(getString(R.string.SRV_UPDATE_STOPED));
		Intent broadcastIntent = new Intent(Const.BROADCAST_MESSAGE).putExtra(Const.SERVICE_UPDATE_STOPED, Const.SERVICE_UPDATE_STOPED);
		sendBroadcast(broadcastIntent);

		WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		if (wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(false);
		}
	}
}
